package com.sheng.dao.user;

import com.sheng.dao.BaseDao;
import com.sheng.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Objects;

public class UserDaoImpl implements UserDao {
    @Override
    public User getLoginUser(Connection connection, String userCode) throws Exception {

        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;

        if (connection != null) {
            String sql = "select * from smbms_user where userCode=?";
            Object[] params = {userCode};

            rs = BaseDao.executeQuery(connection, sql, params, rs, pstm);
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(rs.getInt("userRole"));
                user.setCreatedBy(rs.getInt("createdBy"));
                user.setCreationDate(rs.getTimestamp("creationDate"));
                user.setModifyBy(rs.getInt("modifyBy"));
                user.setModifyDate(rs.getTimestamp("modifyDate"));
            }
            BaseDao.close(null, rs, pstm);

        }


        return user;
    }

    @Override
    public int updatePwd(Connection connection, int id, String pwd) throws Exception {
        int updateRow = 0;
        connection = BaseDao.getConnection();
        PreparedStatement pstm = null;
        String sql = "update smbms_user set userPassword = ? where id = ? ";
        Object[] params = {pwd, id};
        if (connection != null) {
            updateRow = BaseDao.executeQuery(connection, sql, params, pstm);
        }
        BaseDao.close(null, null, pstm);
        return updateRow;
    }
}
